home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_5.lha / 5_5 / 5_5cOb.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  68 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. ain(int, char)
  6.  
  7.    char *s = "123/4+123*4-3";
  8.    expr x(s);
  9.    x.print();
  10.    cout << "\n";
  11.    return 0;
  12.  
  13.  
  14. / print the expression tree
  15. tatic char *spaces(int num)
  16.  
  17.    return form("%.*s", num * 3, " ");
  18.  
  19.  
  20. oid tree::print(int level)
  21.  
  22.    if (this)
  23. switch (this->type)
  24.     {
  25.     case PLUS:
  26.     case DIV:
  27.     case MUL:
  28.     cout << spaces(level) << chr(this->type) << "\n";
  29.     this->left->print(level + 1);
  30.     this->right->print(level + 1);
  31.     return;
  32.  
  33.     case MINUS:
  34.     cout << spaces(level) << chr(this->type) << "\n";
  35.     if (this->right)
  36.         {
  37.         this->left->print(level + 1);
  38.         this->right->print(level + 1);
  39.         }
  40.     else
  41.         this->left->print(level + 1);
  42.     return;
  43.  
  44.     case NUMBER:
  45.     cout << spaces(level) << "# " << this->value << "\n";
  46.     return;
  47.  
  48.     case LP:
  49.     cout << spaces(level) << chr(this->type) << "\n";
  50.     this->left->print(level + 1);
  51.     return;
  52.  
  53.     case RP:
  54.     case END:
  55.     default:
  56.     cout << spaces(level) << chr(this->type) << "\n";
  57.     error("invalid type within tree");
  58.     break;
  59.     }
  60.  
  61.    else
  62. error("NULL node found");
  63.    return;
  64.  
  65.  
  66. oid expr::eprint()
  67.  head->print(0); }
  68.